package com.datawale.vh.demo.work.controller;

import com.alibaba.fastjson.JSONObject;
import com.datawale.vh.demo.work.domain.Avatar;
import com.datawale.vh.demo.work.query.AvatarQuery;
import com.datawale.vh.demo.work.service.AvatarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author huangtao
 * @time 2023/6/8
 * @description
 */
@Controller
@RequestMapping("/avatar")
public class AvatarController {

    @Autowired
    private AvatarService avatarService;

    @RequestMapping(value = "/getOnes",method = RequestMethod.POST)
    @ResponseBody
    public String getOnes(){
        Avatar avatar = avatarService.selectOne();
        return avatar.toString();
    }

    @RequestMapping("/getOne")
    @ResponseBody
    public String getOne(){
        Avatar avatar = avatarService.selectOne();
        return avatar.toString();
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public String getAll(){
        List<Avatar> avatars = avatarService.selectAll();
        System.out.println(avatars.toArray().length);
        return avatars.listIterator().toString();
    }

    @RequestMapping("/getCount")
    @ResponseBody
    public Long getCount(){
        AvatarQuery query = AvatarQuery.builder().status(1).build();
        Long count = avatarService.count(query);
        return count;
    }

    @RequestMapping("/insertAvatar")
    @ResponseBody
    public Long insertAvatar(){
        Avatar avatar = new Avatar();
        avatar.setEnv("prod");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("avatar","/mnt/go.mp4");
        jsonObject.put("video","/mnt/1.mp3");
        avatar.setConfig(jsonObject.toString());

        Long insert = avatarService.insert(avatar);
        return insert;
    }

    @RequestMapping("/updateAvatar")
    @ResponseBody
    public void updateAvatar(Avatar avatar){
        avatar.setId(1L);
        avatar.setStatus(2);
        int update = avatarService.update(avatar);
    }
}
